<!doctype html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>picbed admin</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/login.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./lib/jquery/jquery-3.4.1.min.js"></script>
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script src="./js/xadmin.js"></script>
    <script src="./js/common.js"></script>
    <script src="./js/jquery.base64.js"></script>
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body class="login-bg">

    <div class="login layui-anim layui-anim-up">
        <div class="message">picbed-admin-管理员登录</div>
        <div id="darkbannerwrap"></div>

        <form method="post" class="layui-form" lay-filter="login-form">
            <input name="username" placeholder="用户名" type="text" lay-verify="username" class="layui-input">
            <hr class="hr15">
            <input name="password" lay-verify="password" placeholder="密码" type="password" class="layui-input">
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
            <hr class="hr20">
            <a href="javascript:;" id="forgetpwd" class="layui-form-label">忘记密码？</a>
        </form>
    </div>

    <script>
        $(function () {
            layui.use(['form', 'layer'], function () {
                var form = layui.form,
                    url = window.config.api_url + '/login',
                    layer = layui.layer;
                //自定义验证规则
                form.verify({
                    username: function (value) {
                        if (!value) {
                            return '请输入用户名';
                        }
                    },
                    password: function (value) {
                        if (!value) {
                            return '请输入密码';
                        }
                    }
                });
                // console.log(form.val('login-form'));return false;

                //监听提交
                form.on('submit(login)', function (data) {
                    // console.log(data);

                    var password = $.base64.btoa(data.field.password);
                    var username = $.base64.btoa(data.field.username);
                    $(data.elem).attr('disabled', true);
                    $.ajax({
                        url: url,
                        method: 'post',
                        data: {username: username, password: password},
                        dataType: 'json',
                        processData: true,
                        cache: false,
                        headers: {rtime: window.func.getTime()},
                        success: function (res) {
                            if (res.code == 0) {
                                localStorage.setItem('token', res.data.token);
                                localStorage.setItem('token_expire', res.data.expire);
                                delete res.data.token;
                                delete res.data.expire;
                                localStorage.setItem('ad_user_info', JSON.stringify(res.data));
                                console.log(res);
                                layer.msg('登录成功，跳转中 ... ', { icon: 1, time: 1000 })
                                location.href = './index.html';
                            } else {
                                layer.msg(res.msg, { icon: 2, time: 1000 });
                            }
                            $(data.elem).attr('disabled', false);
                        },
                        error: function () {
                            window.func.error();
                            $(data.elem).attr('disabled', false);
                        }
                    });
                    return false;
                });
                
                // 打开忘记密码页面
                $('#forgetpwd').on('click', function() {
                    xadmin.open('重置密码', './forgetpassword.html', '500', '350');
                });
                
            });
        })
    </script>
    <!-- 底部结束 -->
</body>

</html>